package com.gs.api.weixin.web;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.gs.api.util.EmojiFilterUtil;
import com.gs.api.weixin.entity.WxUser;
import com.gs.api.weixin.service.WxUserService;
import com.gs.api.core.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;

/**
 * @author guoyunfei on 2021/7/10 15:29
 * @className WxUserController
 * @description TODO
 * @Version 1.0
 */
@Api(tags = "微信用户信息")
@RestController
@RequestMapping("{appid}/user")
public class WxUserController {

    @Autowired
    private WxUserService wxUserService;


    @ApiOperation(value = "添加微信用户登录", notes="添加微信用户登录")

    @PostMapping("insertWxUser")
    public Result insertWxUser(WxUser wxUser){

        int insert = wxUserService.insert(wxUser);

        return Result.success(insert);
    }

    @ApiOperation(value = "修改微信用户登录", notes="修改微信用户登录")    @ApiImplicitParams({
            @ApiImplicitParam(name = "appid", value = "微信appid", required = true, dataType = "String"),
    })

    @PostMapping("updateWxUser")
    public Result updateWxUser(@RequestBody  WxUser wxUser,@PathVariable String appid){
        wxUser.setAppid(appid);
        String s = EmojiFilterUtil.filterEmoji(wxUser.getNickName());
        wxUser.setNickName(s);
        int count = wxUserService.update(wxUser);

        return Result.success(count);
    }

    @PostMapping("saveUserInfo")
    public Result saveUserInfo(@RequestBody  WxUser wxUser,@PathVariable String appid){
        wxUser.setAppid(appid);
        String s = EmojiFilterUtil.filterEmoji(wxUser.getNickName());
        wxUser.setNickName(s);
        wxUser.setRegTime(new Date());
        WxUser wu = wxUserService.getOne(new QueryWrapper<WxUser>().eq("openid", wxUser.getOpenid()));
        if(null != wu){
            wxUserService.update(wxUser);
            return Result.success(wu);
        }
        wxUserService.save(wxUser);
        return Result.success(wxUser);
    }

}
